{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Adding and Removing Data\n",
    "We will be working with the `data/earthquakes.csv` file again, so we need to handle our imports and read it in.\n",
    "\n",
    "## About the Data\n",
    "In this notebook, we will be working with Earthquake data from September 18, 2018 - October 13, 2018 (obtained from the US Geological Survey (USGS) using the [USGS API](https://earthquake.usgs.gov/fdsnws/event/1/))\n",
    "\n",
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\n",
    "    'data/earthquakes.csv', \n",
    "    usecols=['time', 'title', 'place', 'magType', 'mag', 'alert', 'tsunami']\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating new data\n",
    "### Adding new columns\n",
    "New columns get added to the right of the dataframe and can be a single value:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alert</th>\n",
       "      <th>mag</th>\n",
       "      <th>magType</th>\n",
       "      <th>place</th>\n",
       "      <th>time</th>\n",
       "      <th>title</th>\n",
       "      <th>tsunami</th>\n",
       "      <th>ones</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.35</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475168010</td>\n",
       "      <td>M 1.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.29</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475129610</td>\n",
       "      <td>M 1.3 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.42</td>\n",
       "      <td>ml</td>\n",
       "      <td>8km NE of Aguanga, CA</td>\n",
       "      <td>1539475062610</td>\n",
       "      <td>M 3.4 - 8km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.44</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539474978070</td>\n",
       "      <td>M 0.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.16</td>\n",
       "      <td>md</td>\n",
       "      <td>10km NW of Avenal, CA</td>\n",
       "      <td>1539474716050</td>\n",
       "      <td>M 2.2 - 10km NW of Avenal, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  alert   mag magType                  place           time  \\\n",
       "0   NaN  1.35      ml  9km NE of Aguanga, CA  1539475168010   \n",
       "1   NaN  1.29      ml  9km NE of Aguanga, CA  1539475129610   \n",
       "2   NaN  3.42      ml  8km NE of Aguanga, CA  1539475062610   \n",
       "3   NaN  0.44      ml  9km NE of Aguanga, CA  1539474978070   \n",
       "4   NaN  2.16      md  10km NW of Avenal, CA  1539474716050   \n",
       "\n",
       "                           title  tsunami  ones  \n",
       "0  M 1.4 - 9km NE of Aguanga, CA        0     1  \n",
       "1  M 1.3 - 9km NE of Aguanga, CA        0     1  \n",
       "2  M 3.4 - 8km NE of Aguanga, CA        0     1  \n",
       "3  M 0.4 - 9km NE of Aguanga, CA        0     1  \n",
       "4  M 2.2 - 10km NW of Avenal, CA        0     1  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['ones'] = 1\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "...or a Boolean mask:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alert</th>\n",
       "      <th>mag</th>\n",
       "      <th>magType</th>\n",
       "      <th>place</th>\n",
       "      <th>time</th>\n",
       "      <th>title</th>\n",
       "      <th>tsunami</th>\n",
       "      <th>ones</th>\n",
       "      <th>mag_negative</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.35</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475168010</td>\n",
       "      <td>M 1.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.29</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475129610</td>\n",
       "      <td>M 1.3 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.42</td>\n",
       "      <td>ml</td>\n",
       "      <td>8km NE of Aguanga, CA</td>\n",
       "      <td>1539475062610</td>\n",
       "      <td>M 3.4 - 8km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.44</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539474978070</td>\n",
       "      <td>M 0.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.16</td>\n",
       "      <td>md</td>\n",
       "      <td>10km NW of Avenal, CA</td>\n",
       "      <td>1539474716050</td>\n",
       "      <td>M 2.2 - 10km NW of Avenal, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  alert   mag magType                  place           time  \\\n",
       "0   NaN  1.35      ml  9km NE of Aguanga, CA  1539475168010   \n",
       "1   NaN  1.29      ml  9km NE of Aguanga, CA  1539475129610   \n",
       "2   NaN  3.42      ml  8km NE of Aguanga, CA  1539475062610   \n",
       "3   NaN  0.44      ml  9km NE of Aguanga, CA  1539474978070   \n",
       "4   NaN  2.16      md  10km NW of Avenal, CA  1539474716050   \n",
       "\n",
       "                           title  tsunami  ones  mag_negative  \n",
       "0  M 1.4 - 9km NE of Aguanga, CA        0     1         False  \n",
       "1  M 1.3 - 9km NE of Aguanga, CA        0     1         False  \n",
       "2  M 3.4 - 8km NE of Aguanga, CA        0     1         False  \n",
       "3  M 0.4 - 9km NE of Aguanga, CA        0     1         False  \n",
       "4  M 2.2 - 10km NW of Avenal, CA        0     1         False  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['mag_negative'] = df.mag < 0\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Adding the `parsed_place` column\n",
    "We have an entity recognition problem on our hands with the `place` column. There are several entities that have multiple names in the data (e.g., CA and California, NV and Nevada)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Afghanistan', 'Alaska', 'Argentina', 'Arizona', 'Arkansas',\n",
       "       'Australia', 'Azerbaijan', 'B.C., MX', 'Barbuda', 'Bolivia',\n",
       "       'Bonaire, Saint Eustatius and Saba ', 'British Virgin Islands',\n",
       "       'Burma', 'CA', 'California', 'Canada', 'Chile', 'China',\n",
       "       'Christmas Island', 'Colombia', 'Colorado', 'Costa Rica',\n",
       "       'Dominican Republic', 'East Timor', 'Ecuador', 'Ecuador region',\n",
       "       'El Salvador', 'Fiji', 'Greece', 'Greenland', 'Guam', 'Guatemala',\n",
       "       'Haiti', 'Hawaii', 'Honduras', 'Idaho', 'Illinois', 'India',\n",
       "       'Indonesia', 'Iran', 'Iraq', 'Italy', 'Jamaica', 'Japan', 'Kansas',\n",
       "       'Kentucky', 'Kyrgyzstan', 'Martinique', 'Mauritius', 'Mayotte',\n",
       "       'Mexico', 'Missouri', 'Montana', 'NV', 'Nevada', 'New Caledonia',\n",
       "       'New Hampshire', 'New Mexico', 'New Zealand', 'Nicaragua',\n",
       "       'North Carolina', 'Northern Mariana Islands', 'Oklahoma', 'Oregon',\n",
       "       'Pakistan', 'Papua New Guinea', 'Peru', 'Philippines',\n",
       "       'Puerto Rico', 'Romania', 'Russia', 'Saint Helena',\n",
       "       'Solomon Islands', 'Somalia', 'South Africa', 'South Carolina',\n",
       "       'South Georgia and the South Sandwich Islands',\n",
       "       'South Sandwich Islands', 'Taiwan', 'Tajikistan', 'Tennessee',\n",
       "       'Texas', 'Tonga', 'Turkey', 'U.S. Virgin Islands', 'Utah',\n",
       "       'Uzbekistan', 'Vanuatu', 'Vermont', 'Washington', 'Wyoming',\n",
       "       'Yemen', nan], dtype=object)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.place.str.extract(r', (.*$)')[0].sort_values().unique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Replace parts of the `place` names to fit our needs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['parsed_place'] = df.place.str.replace(\n",
    "    r'.* of ', '' # remove anything saying <something> of <something>\n",
    ").str.replace(\n",
    "    r'the ', '' # remove things starting with \"the\"\n",
    ").str.replace(\n",
    "    r'CA$', 'California' # fix California\n",
    ").str.replace(\n",
    "    r'NV$', 'Nevada' # fix Nevada\n",
    ").str.replace(\n",
    "    r'MX$', 'Mexico' # fix Mexico\n",
    ").str.replace(\n",
    "    r' region$', '' # chop off endings with \"region\"\n",
    ").str.replace(\n",
    "    r'northern ', '' # remove \"northern\"\n",
    ").str.replace(\n",
    "    r'Fiji Islands', 'Fiji' # line up the Fiji places\n",
    ").str.replace(\n",
    "    r'^.*, ', '' # remove anything else extraneous from the beginning\n",
    ").str.strip() # remove any extra spaces"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can use a single name to get all earthquakes for that place:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Afghanistan', 'Alaska', 'Argentina', 'Arizona', 'Arkansas',\n",
       "       'Ascension Island', 'Australia', 'Azerbaijan', 'Balleny Islands',\n",
       "       'Barbuda', 'Bolivia', 'British Virgin Islands', 'Burma',\n",
       "       'California', 'Canada', 'Carlsberg Ridge',\n",
       "       'Central East Pacific Rise', 'Central Mid-Atlantic Ridge', 'Chile',\n",
       "       'China', 'Christmas Island', 'Colombia', 'Colorado', 'Costa Rica',\n",
       "       'Dominican Republic', 'East Timor', 'Ecuador', 'El Salvador',\n",
       "       'Fiji', 'Greece', 'Greenland', 'Guam', 'Guatemala', 'Haiti',\n",
       "       'Hawaii', 'Honduras', 'Idaho', 'Illinois', 'India',\n",
       "       'Indian Ocean Triple Junction', 'Indonesia', 'Iran', 'Iraq',\n",
       "       'Italy', 'Jamaica', 'Japan', 'Kansas', 'Kentucky',\n",
       "       'Kermadec Islands', 'Kuril Islands', 'Kyrgyzstan', 'Martinique',\n",
       "       'Mauritius', 'Mayotte', 'Mexico', 'Mid-Indian Ridge', 'Missouri',\n",
       "       'Montana', 'Nevada', 'New Caledonia', 'New Hampshire',\n",
       "       'New Mexico', 'New Zealand', 'Nicaragua', 'North Carolina',\n",
       "       'Northern East Pacific Rise', 'Northern Mariana Islands',\n",
       "       'Northern Mid-Atlantic Ridge', 'Oklahoma', 'Oregon',\n",
       "       'Pacific-Antarctic Ridge', 'Pakistan', 'Papua New Guinea', 'Peru',\n",
       "       'Philippines', 'Prince Edward Islands', 'Puerto Rico',\n",
       "       'Queen Charlotte Islands', 'Romania', 'Russia',\n",
       "       'Saint Eustatius and Saba', 'Saint Helena', 'Socotra',\n",
       "       'Solomon Islands', 'Somalia', 'South Africa', 'South Carolina',\n",
       "       'South Georgia and South Sandwich Islands',\n",
       "       'South Sandwich Islands', 'Southeast Indian Ridge',\n",
       "       'Southern East Pacific Rise', 'Southern Mid-Atlantic Ridge',\n",
       "       'Southwest Indian Ridge', 'Sumatra', 'Taiwan', 'Tajikistan',\n",
       "       'Tennessee', 'Texas', 'Tonga', 'Turkey', 'U.S. Virgin Islands',\n",
       "       'Utah', 'Uzbekistan', 'Vanuatu', 'Vermont', 'Washington',\n",
       "       'Western Indian-Antarctic Ridge', 'Western Xizang', 'Wyoming',\n",
       "       'Yemen'], dtype=object)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.parsed_place.sort_values().unique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Using the `assign()` method to create columns\n",
    "To create many columns at once or update existing columns, we can use `assign()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alert</th>\n",
       "      <th>mag</th>\n",
       "      <th>magType</th>\n",
       "      <th>place</th>\n",
       "      <th>time</th>\n",
       "      <th>title</th>\n",
       "      <th>tsunami</th>\n",
       "      <th>ones</th>\n",
       "      <th>mag_negative</th>\n",
       "      <th>parsed_place</th>\n",
       "      <th>in_ca</th>\n",
       "      <th>in_alaska</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.35</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475168010</td>\n",
       "      <td>M 1.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.29</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475129610</td>\n",
       "      <td>M 1.3 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.42</td>\n",
       "      <td>ml</td>\n",
       "      <td>8km NE of Aguanga, CA</td>\n",
       "      <td>1539475062610</td>\n",
       "      <td>M 3.4 - 8km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.44</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539474978070</td>\n",
       "      <td>M 0.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.16</td>\n",
       "      <td>md</td>\n",
       "      <td>10km NW of Avenal, CA</td>\n",
       "      <td>1539474716050</td>\n",
       "      <td>M 2.2 - 10km NW of Avenal, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  alert   mag magType                  place           time  \\\n",
       "0   NaN  1.35      ml  9km NE of Aguanga, CA  1539475168010   \n",
       "1   NaN  1.29      ml  9km NE of Aguanga, CA  1539475129610   \n",
       "2   NaN  3.42      ml  8km NE of Aguanga, CA  1539475062610   \n",
       "3   NaN  0.44      ml  9km NE of Aguanga, CA  1539474978070   \n",
       "4   NaN  2.16      md  10km NW of Avenal, CA  1539474716050   \n",
       "\n",
       "                           title  tsunami  ones  mag_negative parsed_place  \\\n",
       "0  M 1.4 - 9km NE of Aguanga, CA        0     1         False   California   \n",
       "1  M 1.3 - 9km NE of Aguanga, CA        0     1         False   California   \n",
       "2  M 3.4 - 8km NE of Aguanga, CA        0     1         False   California   \n",
       "3  M 0.4 - 9km NE of Aguanga, CA        0     1         False   California   \n",
       "4  M 2.2 - 10km NW of Avenal, CA        0     1         False   California   \n",
       "\n",
       "   in_ca  in_alaska  \n",
       "0   True      False  \n",
       "1   True      False  \n",
       "2   True      False  \n",
       "3   True      False  \n",
       "4   True      False  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.assign(\n",
    "    in_ca=df.parsed_place.str.endswith('California'),\n",
    "    in_alaska=df.parsed_place.str.endswith('Alaska')\n",
    ").head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Concatenation\n",
    "Say we were working with two separate dataframes, one with earthquakes accompanied by tsunamis and the other with earthquakes without tsunamis. If we wanted to look at earthquakes as a whole, we would want to concatenate the dataframes into a single one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((61, 10), (9271, 10))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsunami = df[df.tsunami == 1]\n",
    "no_tsunami = df[df.tsunami == 0]\n",
    "\n",
    "tsunami.shape, no_tsunami.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Concatenating along the row axis (`axis=0`) is equivalent to appending to the bottom. By concatenating our earthquakes with tsunamis and those without tsunamis, we get the full earthquake data set back:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9332, 10)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([tsunami, no_tsunami]).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the previous result is equivalent to running the `append()` method of the dataframe:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9332, 10)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsunami.append(no_tsunami).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have been working with a subset of the columns from the CSV file, but now we want to get some of the columns we ignored when we read in the data. Since we have added new columns in this notebook, we won't want to read in the file again and perform those operations again. Instead, we will concatenate along the columns (`axis=1`) to add back what we are missing:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alert</th>\n",
       "      <th>mag</th>\n",
       "      <th>magType</th>\n",
       "      <th>place</th>\n",
       "      <th>time</th>\n",
       "      <th>title</th>\n",
       "      <th>tsunami</th>\n",
       "      <th>ones</th>\n",
       "      <th>mag_negative</th>\n",
       "      <th>parsed_place</th>\n",
       "      <th>felt</th>\n",
       "      <th>ids</th>\n",
       "      <th>tz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.35</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475168010</td>\n",
       "      <td>M 1.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "      <td>NaN</td>\n",
       "      <td>,ci37389218,</td>\n",
       "      <td>-480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.29</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475129610</td>\n",
       "      <td>M 1.3 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "      <td>NaN</td>\n",
       "      <td>,ci37389202,</td>\n",
       "      <td>-480.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  alert   mag magType                  place           time  \\\n",
       "0   NaN  1.35      ml  9km NE of Aguanga, CA  1539475168010   \n",
       "1   NaN  1.29      ml  9km NE of Aguanga, CA  1539475129610   \n",
       "\n",
       "                           title  tsunami  ones  mag_negative parsed_place  \\\n",
       "0  M 1.4 - 9km NE of Aguanga, CA        0     1         False   California   \n",
       "1  M 1.3 - 9km NE of Aguanga, CA        0     1         False   California   \n",
       "\n",
       "   felt           ids     tz  \n",
       "0   NaN  ,ci37389218, -480.0  \n",
       "1   NaN  ,ci37389202, -480.0  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "additional_columns = pd.read_csv(\n",
    "    'data/earthquakes.csv', usecols=['tz', 'felt', 'ids']\n",
    ")\n",
    "pd.concat(\n",
    "    [df.head(2), additional_columns.head(2)], axis=1\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice what happens if the index doesn't align though:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alert</th>\n",
       "      <th>mag</th>\n",
       "      <th>magType</th>\n",
       "      <th>place</th>\n",
       "      <th>time</th>\n",
       "      <th>title</th>\n",
       "      <th>tsunami</th>\n",
       "      <th>ones</th>\n",
       "      <th>mag_negative</th>\n",
       "      <th>parsed_place</th>\n",
       "      <th>felt</th>\n",
       "      <th>ids</th>\n",
       "      <th>tz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.35</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1.539475e+12</td>\n",
       "      <td>M 1.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.29</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1.539475e+12</td>\n",
       "      <td>M 1.3 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1539475129610</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>,ci37389202,</td>\n",
       "      <td>-480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1539475168010</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>,ci37389218,</td>\n",
       "      <td>-480.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              alert   mag magType                  place          time  \\\n",
       "0               NaN  1.35      ml  9km NE of Aguanga, CA  1.539475e+12   \n",
       "1               NaN  1.29      ml  9km NE of Aguanga, CA  1.539475e+12   \n",
       "1539475129610   NaN   NaN     NaN                    NaN           NaN   \n",
       "1539475168010   NaN   NaN     NaN                    NaN           NaN   \n",
       "\n",
       "                                       title  tsunami  ones mag_negative  \\\n",
       "0              M 1.4 - 9km NE of Aguanga, CA      0.0   1.0        False   \n",
       "1              M 1.3 - 9km NE of Aguanga, CA      0.0   1.0        False   \n",
       "1539475129610                            NaN      NaN   NaN          NaN   \n",
       "1539475168010                            NaN      NaN   NaN          NaN   \n",
       "\n",
       "              parsed_place  felt           ids     tz  \n",
       "0               California   NaN           NaN    NaN  \n",
       "1               California   NaN           NaN    NaN  \n",
       "1539475129610          NaN   NaN  ,ci37389202, -480.0  \n",
       "1539475168010          NaN   NaN  ,ci37389218, -480.0  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "additional_columns = pd.read_csv(\n",
    "    'data/earthquakes.csv', usecols=['tz', 'felt', 'ids', 'time'], index_col='time'\n",
    ")\n",
    "pd.concat(\n",
    "    [df.head(2), additional_columns.head(2)], axis=1\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the index doesn't align, we can align it before attempting the concatentation, which we will discuss in [chapter 3](https://github.com/stefmolin/Hands-On-Data-Analysis-with-Pandas/tree/master/ch_03):\n",
    "\n",
    "Say we want to join the `tsunami` and `no_tsunami` dataframes, but the `no_tsunami` dataframe has an additional column. The `join` parameter specifies how to handle any overlap in column names (when appending to the bottom) or in row names (when concatenating to the left/right). By default, this is `outer`, so we keep everything; however, if we use `inner`, we will only keep what is in common:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alert</th>\n",
       "      <th>mag</th>\n",
       "      <th>magType</th>\n",
       "      <th>place</th>\n",
       "      <th>time</th>\n",
       "      <th>title</th>\n",
       "      <th>tsunami</th>\n",
       "      <th>ones</th>\n",
       "      <th>mag_negative</th>\n",
       "      <th>parsed_place</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>NaN</td>\n",
       "      <td>5.00</td>\n",
       "      <td>mww</td>\n",
       "      <td>165km NNW of Flying Fish Cove, Christmas Island</td>\n",
       "      <td>1539459504090</td>\n",
       "      <td>M 5.0 - 165km NNW of Flying Fish Cove, Christm...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>Christmas Island</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>118</th>\n",
       "      <td>green</td>\n",
       "      <td>6.70</td>\n",
       "      <td>mww</td>\n",
       "      <td>262km NW of Ozernovskiy, Russia</td>\n",
       "      <td>1539429023560</td>\n",
       "      <td>M 6.7 - 262km NW of Ozernovskiy, Russia</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>Russia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.35</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475168010</td>\n",
       "      <td>M 1.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.29</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475129610</td>\n",
       "      <td>M 1.3 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     alert   mag magType                                            place  \\\n",
       "36     NaN  5.00     mww  165km NNW of Flying Fish Cove, Christmas Island   \n",
       "118  green  6.70     mww                  262km NW of Ozernovskiy, Russia   \n",
       "0      NaN  1.35      ml                            9km NE of Aguanga, CA   \n",
       "1      NaN  1.29      ml                            9km NE of Aguanga, CA   \n",
       "\n",
       "              time                                              title  \\\n",
       "36   1539459504090  M 5.0 - 165km NNW of Flying Fish Cove, Christm...   \n",
       "118  1539429023560            M 6.7 - 262km NW of Ozernovskiy, Russia   \n",
       "0    1539475168010                      M 1.4 - 9km NE of Aguanga, CA   \n",
       "1    1539475129610                      M 1.3 - 9km NE of Aguanga, CA   \n",
       "\n",
       "     tsunami  ones  mag_negative      parsed_place  \n",
       "36         1     1         False  Christmas Island  \n",
       "118        1     1         False            Russia  \n",
       "0          0     1         False        California  \n",
       "1          0     1         False        California  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat(\n",
    "    [tsunami.head(2), no_tsunami.head(2).assign(type='earthquake')], join='inner'\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition, we use `ignore_index`, since the index doesn't mean anything for us here. This gives us sequential values instead of what we had in the previous result:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alert</th>\n",
       "      <th>mag</th>\n",
       "      <th>magType</th>\n",
       "      <th>place</th>\n",
       "      <th>time</th>\n",
       "      <th>title</th>\n",
       "      <th>tsunami</th>\n",
       "      <th>ones</th>\n",
       "      <th>mag_negative</th>\n",
       "      <th>parsed_place</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>5.00</td>\n",
       "      <td>mww</td>\n",
       "      <td>165km NNW of Flying Fish Cove, Christmas Island</td>\n",
       "      <td>1539459504090</td>\n",
       "      <td>M 5.0 - 165km NNW of Flying Fish Cove, Christm...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>Christmas Island</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>green</td>\n",
       "      <td>6.70</td>\n",
       "      <td>mww</td>\n",
       "      <td>262km NW of Ozernovskiy, Russia</td>\n",
       "      <td>1539429023560</td>\n",
       "      <td>M 6.7 - 262km NW of Ozernovskiy, Russia</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>Russia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.35</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475168010</td>\n",
       "      <td>M 1.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.29</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539475129610</td>\n",
       "      <td>M 1.3 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>California</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   alert   mag magType                                            place  \\\n",
       "0    NaN  5.00     mww  165km NNW of Flying Fish Cove, Christmas Island   \n",
       "1  green  6.70     mww                  262km NW of Ozernovskiy, Russia   \n",
       "2    NaN  1.35      ml                            9km NE of Aguanga, CA   \n",
       "3    NaN  1.29      ml                            9km NE of Aguanga, CA   \n",
       "\n",
       "            time                                              title  tsunami  \\\n",
       "0  1539459504090  M 5.0 - 165km NNW of Flying Fish Cove, Christm...        1   \n",
       "1  1539429023560            M 6.7 - 262km NW of Ozernovskiy, Russia        1   \n",
       "2  1539475168010                      M 1.4 - 9km NE of Aguanga, CA        0   \n",
       "3  1539475129610                      M 1.3 - 9km NE of Aguanga, CA        0   \n",
       "\n",
       "   ones  mag_negative      parsed_place  \n",
       "0     1         False  Christmas Island  \n",
       "1     1         False            Russia  \n",
       "2     1         False        California  \n",
       "3     1         False        California  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat(\n",
    "    [tsunami.head(2), no_tsunami.head(2).assign(type='earthquake')], join='inner', ignore_index=True\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deleting Unwanted Data\n",
    "Columns can be deleted using dictionary syntax with `del`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['alert', 'mag', 'magType', 'place', 'time', 'title', 'tsunami',\n",
       "       'mag_negative', 'parsed_place'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del df['ones']\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we don't know if the column exists, we can use a `try`/`except` block:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "not there anymore\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    del df['ones']\n",
    "except KeyError:\n",
    "    # handle the error here\n",
    "    print('not there anymore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also use `pop()`. This will allow us to use the series we remove later. Note there will be an error if the key doesn't exist, so we can also use a `try`/`except` here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['alert', 'mag', 'magType', 'place', 'time', 'title', 'tsunami',\n",
       "       'parsed_place'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mag_negative = df.pop('mag_negative')\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice we have a mask in `mag_negative` now:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False    8841\n",
       "True      491\n",
       "Name: mag_negative, dtype: int64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mag_negative.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can use `mag_negative` to select:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alert</th>\n",
       "      <th>mag</th>\n",
       "      <th>magType</th>\n",
       "      <th>place</th>\n",
       "      <th>time</th>\n",
       "      <th>title</th>\n",
       "      <th>tsunami</th>\n",
       "      <th>parsed_place</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.10</td>\n",
       "      <td>ml</td>\n",
       "      <td>6km NW of Lemmon Valley, Nevada</td>\n",
       "      <td>1539458844506</td>\n",
       "      <td>M -0.1 - 6km NW of Lemmon Valley, Nevada</td>\n",
       "      <td>0</td>\n",
       "      <td>Nevada</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.10</td>\n",
       "      <td>ml</td>\n",
       "      <td>6km NW of Lemmon Valley, Nevada</td>\n",
       "      <td>1539455017464</td>\n",
       "      <td>M -0.1 - 6km NW of Lemmon Valley, Nevada</td>\n",
       "      <td>0</td>\n",
       "      <td>Nevada</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>135</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.40</td>\n",
       "      <td>ml</td>\n",
       "      <td>10km SSE of Beatty, Nevada</td>\n",
       "      <td>1539422175717</td>\n",
       "      <td>M -0.4 - 10km SSE of Beatty, Nevada</td>\n",
       "      <td>0</td>\n",
       "      <td>Nevada</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>161</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>md</td>\n",
       "      <td>20km SSE of Ronan, Montana</td>\n",
       "      <td>1539412475360</td>\n",
       "      <td>M -0.0 - 20km SSE of Ronan, Montana</td>\n",
       "      <td>0</td>\n",
       "      <td>Montana</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>198</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.20</td>\n",
       "      <td>ml</td>\n",
       "      <td>60km N of Pahrump, Nevada</td>\n",
       "      <td>1539398340822</td>\n",
       "      <td>M -0.2 - 60km N of Pahrump, Nevada</td>\n",
       "      <td>0</td>\n",
       "      <td>Nevada</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    alert   mag magType                            place           time  \\\n",
       "39    NaN -0.10      ml  6km NW of Lemmon Valley, Nevada  1539458844506   \n",
       "49    NaN -0.10      ml  6km NW of Lemmon Valley, Nevada  1539455017464   \n",
       "135   NaN -0.40      ml       10km SSE of Beatty, Nevada  1539422175717   \n",
       "161   NaN -0.02      md       20km SSE of Ronan, Montana  1539412475360   \n",
       "198   NaN -0.20      ml        60km N of Pahrump, Nevada  1539398340822   \n",
       "\n",
       "                                        title  tsunami parsed_place  \n",
       "39   M -0.1 - 6km NW of Lemmon Valley, Nevada        0       Nevada  \n",
       "49   M -0.1 - 6km NW of Lemmon Valley, Nevada        0       Nevada  \n",
       "135       M -0.4 - 10km SSE of Beatty, Nevada        0       Nevada  \n",
       "161       M -0.0 - 20km SSE of Ronan, Montana        0      Montana  \n",
       "198        M -0.2 - 60km N of Pahrump, Nevada        0       Nevada  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[mag_negative].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Using the `drop()` method\n",
    "We can drop rows by passing a list of indices to the `drop()` method. Notice in the following example that when asking for the first 2 rows with `head()` we get the 3rd and 4th rows because we dropped the original first 2 with `drop([0, 1])`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alert</th>\n",
       "      <th>mag</th>\n",
       "      <th>magType</th>\n",
       "      <th>place</th>\n",
       "      <th>time</th>\n",
       "      <th>title</th>\n",
       "      <th>tsunami</th>\n",
       "      <th>parsed_place</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.42</td>\n",
       "      <td>ml</td>\n",
       "      <td>8km NE of Aguanga, CA</td>\n",
       "      <td>1539475062610</td>\n",
       "      <td>M 3.4 - 8km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>California</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.44</td>\n",
       "      <td>ml</td>\n",
       "      <td>9km NE of Aguanga, CA</td>\n",
       "      <td>1539474978070</td>\n",
       "      <td>M 0.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "      <td>California</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  alert   mag magType                  place           time  \\\n",
       "2   NaN  3.42      ml  8km NE of Aguanga, CA  1539475062610   \n",
       "3   NaN  0.44      ml  9km NE of Aguanga, CA  1539474978070   \n",
       "\n",
       "                           title  tsunami parsed_place  \n",
       "2  M 3.4 - 8km NE of Aguanga, CA        0   California  \n",
       "3  M 0.4 - 9km NE of Aguanga, CA        0   California  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop([0, 1]).head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `drop()` method drops along the row axis by default. If we pass in `axis=1` for the column axis, we can delete columns:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alert</th>\n",
       "      <th>mag</th>\n",
       "      <th>time</th>\n",
       "      <th>title</th>\n",
       "      <th>tsunami</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.35</td>\n",
       "      <td>1539475168010</td>\n",
       "      <td>M 1.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.29</td>\n",
       "      <td>1539475129610</td>\n",
       "      <td>M 1.3 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.42</td>\n",
       "      <td>1539475062610</td>\n",
       "      <td>M 3.4 - 8km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.44</td>\n",
       "      <td>1539474978070</td>\n",
       "      <td>M 0.4 - 9km NE of Aguanga, CA</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.16</td>\n",
       "      <td>1539474716050</td>\n",
       "      <td>M 2.2 - 10km NW of Avenal, CA</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  alert   mag           time                          title  tsunami\n",
       "0   NaN  1.35  1539475168010  M 1.4 - 9km NE of Aguanga, CA        0\n",
       "1   NaN  1.29  1539475129610  M 1.3 - 9km NE of Aguanga, CA        0\n",
       "2   NaN  3.42  1539475062610  M 3.4 - 8km NE of Aguanga, CA        0\n",
       "3   NaN  0.44  1539474978070  M 0.4 - 9km NE of Aguanga, CA        0\n",
       "4   NaN  2.16  1539474716050  M 2.2 - 10km NW of Avenal, CA        0"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(\n",
    "    columns=[col for col in df.columns \\\n",
    "             if col not in \\\n",
    "             ['alert', 'mag', 'title', 'time', 'tsunami']]\n",
    ").head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We also have the option of passing the column names to the `columns` parameter instead of using `axis=1`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(\n",
    "    columns=[col for col in df.columns \\\n",
    "             if col not in \\\n",
    "             ['alert', 'mag', 'title', 'time', 'tsunami']]\n",
    ").equals(\n",
    "    df.drop(\n",
    "        [col for col in df.columns \\\n",
    "         if col not in ['alert', 'mag', 'title', 'time', 'tsunami']],\n",
    "        axis=1\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, `drop()`, along with the majority of `DataFrame` methods, will return a new `DataFrame` object. If we just want to change the one we are working with, we can pass `inplace=True`. This should be used with care:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alert</th>\n",
       "      <th>mag</th>\n",
       "      <th>time</th>\n",
       "      <th>tsunami</th>\n",
       "      <th>parsed_place</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.35</td>\n",
       "      <td>1539475168010</td>\n",
       "      <td>0</td>\n",
       "      <td>California</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.29</td>\n",
       "      <td>1539475129610</td>\n",
       "      <td>0</td>\n",
       "      <td>California</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.42</td>\n",
       "      <td>1539475062610</td>\n",
       "      <td>0</td>\n",
       "      <td>California</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.44</td>\n",
       "      <td>1539474978070</td>\n",
       "      <td>0</td>\n",
       "      <td>California</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.16</td>\n",
       "      <td>1539474716050</td>\n",
       "      <td>0</td>\n",
       "      <td>California</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  alert   mag           time  tsunami parsed_place\n",
       "0   NaN  1.35  1539475168010        0   California\n",
       "1   NaN  1.29  1539475129610        0   California\n",
       "2   NaN  3.42  1539475062610        0   California\n",
       "3   NaN  0.44  1539474978070        0   California\n",
       "4   NaN  2.16  1539474716050        0   California"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(\n",
    "    columns=[col for col in df.columns \\\n",
    "             if col not in \\\n",
    "             ['alert', 'mag', 'parsed_place', 'time', 'tsunami']],\n",
    "    inplace=True\n",
    ")\n",
    "df.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
